﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Px = PhysX;

namespace ChimneySim
{
    public class ParticleEmiter
    {
        private List<Particle> particles;
        public List<Particle> Particles
        {
            get { return particles; }
            set { particles = value; }
        }

        private Model particleModel;
        public Model ParticleModel
        {
            get { return particleModel; }
            set { particleModel = value; }
        }

        private int maxParticles;
        public int MaxParticles
        {
            get { return maxParticles; }
            set { maxParticles = value; }
        }

        private float particleMass;
        private Vector3 initialSpeed;

        private TimeSpan particleLifetime;

        Random rand;

        private Vector3 emitterPosition;
        public Vector3 EmitterPosition
        {
            get { return emitterPosition; }
            set { emitterPosition = value; }
        }


        public ParticleEmiter(Vector3 position, int maxParticles, float particleMass, Vector3 initialSpeed, TimeSpan particleLifetime)
        {
            this.maxParticles = maxParticles;
            this.particleMass = particleMass;
            particles = new List<Particle>();
            this.initialSpeed = initialSpeed;
            rand = new Random();
            this.particleLifetime = particleLifetime;
            this.emitterPosition = position;

            CreateParticles();
        }

        private void CreateParticles()
        {
            for (int i = 0; i < maxParticles; i++)
            {                
                particles.Add(CreateNewParticle());
            }
        }

        private Particle CreateNewParticle()
        {
            Vector3 speed = initialSpeed;// +RandVector(0.1f);
            Vector3 position = Vector3.Zero;// +RandVector(0.1f);
            return new Particle(position, speed, particleMass);
        }

        private Vector3 RandVector(float maxVal)
        {
            float x = (float)rand.NextDouble() * maxVal;
            float y = (float)rand.NextDouble() * maxVal;
            float z = (float)rand.NextDouble() * maxVal;

            return new Vector3(x, y, z);
        }

        public void Update(GameTime GameTime)
        {
            //obliczenie pozycji cząstki

            foreach (Particle p in particles)
            {
                p.LifeTime += GameTime.ElapsedGameTime;

                if (p.LifeTime >= particleLifetime)
                {
                    //Zerowanie particla
                    p.Position = Vector3.Zero;// +RandVector(0.1f);
                    p.Speed = initialSpeed + RandVector(0.1f);
                    p.LifeTime = TimeSpan.Zero;
                }

                float delta = (float)GameTime.ElapsedGameTime.TotalSeconds / 10.0f;

                for (int i = 0; i < 10; i++)
                {
                    Vector3 force = new Vector3(0, -0.009f, 0);
                    Vector3 accel = force / p.Mass;

                    Vector3 newSpeed = p.Speed + delta * accel;
                    p.Speed = newSpeed;

                    Vector3 newPosition = p.Position + delta * newSpeed;
                    p.Position = newPosition;
                }
                
            }
        }
    }
}
